var keystone = require('keystone');
var Types = keystone.Field.Types;

/**
 * User Model
 * ==========
 */
var User = new keystone.List('User');

User.add({
    userName: {type: String, required: true, initial: true, index: true},
    // name: {type: Types.Name, required: false, index: true},
    realName: {type: String, required: false, index: false},
    mobile: {type: String, required: false, initial: true, index: true},
    phone: {type: String, required: false, initial: true, index: false},
    email: {type: Types.Email, initial: true, required: true, index: true},
    password: {type: Types.Password, initial: true, required: true},
    project: {type: String, required: false, index: true},
    userGroup: {type: Types.Relationship, ref: 'UserGroup', many: false},
    registerTime: {type: Types.Datetime, default: Date.now},
    loginTime: {type: Types.Datetime, default: Date.now},
    isBindWeiXin: {type: Boolean, label: '是否已经绑定微信', required: false,},
    WeiXinGroup: {type: String, required: false, index: false},
    openid: {type: String, required: false,},
    status: {type: Types.Select, options: 'used, disable, delete, locked', default: 'used'},
    faceImg: {type: String, required: false, index: false},
    other: {type: String, required: false, index: true},
    messageCount: {type: Number, min: 0, default: 0},
}, 'Permissions', {
    isAdmin: {type: Boolean, label: 'Can access Keystone', index: true},
});

// Provide access to Keystone
User.schema.virtual('canAccessKeystone').get(function () {
    return this.isAdmin;
});


/**
 * Relationships
 */
// User.relationship({ref: 'UserGroup', path: 'posts', refPath: 'author'});


/**
 * Registration
 */
User.defaultColumns = 'userName, name, email, isAdmin';
User.register();
